package com.example.apple.paopo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.apple.paopo.R;
import com.example.apple.paopo.base.BaseActivity;
import com.example.apple.paopo.dialog.PaoPoDialog;
import com.vondear.rxtools.RxRegTool;
import com.vondear.rxtools.view.RxToast;
import com.zyao89.view.zloading.ZLoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by apple on 17/12/1.
 */
@ContentView(R.layout.activity_forgetpasswordemail)
public class ForgetPasswordEmailActivity extends BaseActivity {
    @ViewInject(R.id.tool_title)
    TextView toolText;
    @ViewInject(R.id.tool_more)
    ImageView toolMore;
    @ViewInject(R.id.forgetPasswordemail_tv_account)
    EditText account;
    @ViewInject(R.id.forgetPasswordemail_tv_email)
    EditText email;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        toolMore.setVisibility(View.GONE);
        toolText.setText("密码找回界面");
    }
    @Event(R.id.tool_break)
    private void ToolbreakAction(View v){
        finish();
    }

    @Event(R.id.forgetPasswordemail_bt_confirm)
    private  void  forgetPasswordAction(View v ){
        if (account.getText().length()==0){
            RxToast.info("账号不能为空");
            return;
        }
        if (!RxRegTool.isEmail(email.getText().toString())){
            RxToast.info("邮箱不正确，请重新输入");
            return;
        }
        final ZLoadingDialog dialog = PaoPoDialog.getPaoPoDialog(ForgetPasswordEmailActivity.this,"正在发送PAOPO邮件,请稍后...");
        RequestParams params = new RequestParams("http://cloud.bmob.cn/91c9d2dffacfcd37/forgetPasswordAction");
        params.addQueryStringParameter("user",account.getText().toString());
        params.addQueryStringParameter("email",email.getText().toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //解析result
                try {
                    JSONObject jsonObject =new JSONObject(result);
                    if (jsonObject.getString("state").equals("YES")){
                        RxToast.success(jsonObject.getString("msge"));
                        finish();
                    }else {
                        RxToast.error("该账号不存在，请重新输入");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dialog.dismiss();
            }
            //请求异常后的回调方法
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                RxToast.error("发送失败，请检查网络");
                dialog.dismiss();
            }
            //主动调用取消请求的回调方法
            @Override
            public void onCancelled(CancelledException cex) {
                dialog.dismiss();
            }
            @Override
            public void onFinished() {
                dialog.dismiss();
            }
        });

    }
}
